package com.ruoyi.system.mapper;

import com.ruoyi.system.domain.Category;
import com.ruoyi.system.domain.vo.CategorySearchResult;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;

import java.util.List;

@Mapper
public interface CategoryMapper extends BaseMapper<Category> {

    // 自定义查询示例（根据父ID查询）
    @Select("SELECT * FROM tb_category WHERE parent_id = #{parentId} AND del_flag = '0'")
    List<Category> selectByParentId(@Param("parentId") Long parentId);

    @Select("WITH RECURSIVE subcategories AS (" +
            "    SELECT id " +
            "    FROM tb_category " +
            "    WHERE parent_id = #{parentId} AND del_flag = '0' " +
            "    UNION ALL " +
            "    SELECT c.id " +
            "    FROM tb_category c " +
            "    INNER JOIN subcategories s ON c.parent_id = s.id " +
            "    WHERE c.del_flag = '0'" +
            ") " +
            "SELECT id FROM subcategories")
    List<Long> selectAllSubCategoryIds(@Param("parentId") Long parentId);
}
